<?php

class CloudSyncContentModel
{
    private $cloudSyncContentKeyPrefix  =   'CSC/';

    public function cloudSync($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']) || empty($cloudInfo['type']))
        {
            AC::$errorNo    =   'CSCM009';
            return  FALSE;
        }
        $checkType  =   $this->checkType($cloudInfo['type']);
        if (empty($checkType))  return  FALSE;

        $data['uid']        =   $cloudInfo['uid'];
        $data['type']       =   $cloudInfo['type'];
        $data['saveTime']   =   AC::SERVER_TIME;

        isset($cloudInfo['title'])      &&  $data['title']      =   $cloudInfo['title'];
        isset($cloudInfo['content'])    &&  $data['content']    =   $cloudInfo['content'];
        isset($cloudInfo['markDate'])   &&  $data['markDate']   =   $cloudInfo['markDate'];
        isset($cloudInfo['category'])   &&  $data['category']   =   abs($cloudInfo['category']);
        isset($cloudInfo['secret'])     &&  $data['secret']     =   abs($cloudInfo['secret']);
        isset($cloudInfo['weather'])    &&  $data['weather']    =   abs($cloudInfo['weather']);
        isset($cloudInfo['emotion'])    &&  $data['emotion']    =   abs($cloudInfo['emotion']);
        isset($cloudInfo['textSize'])   &&  $data['textSize']   =   abs($cloudInfo['textSize']);
        isset($cloudInfo['textColor'])  &&  $data['textColor']  =   $cloudInfo['textColor'];
        isset($cloudInfo['theme'])      &&  $data['theme']      =   abs($cloudInfo['theme']);
        isset($cloudInfo['posX'])       &&  $data['posX']       =   $cloudInfo['posX'];
        isset($cloudInfo['posY'])       &&  $data['posY']       =   $cloudInfo['posY'];
        isset($cloudInfo['place'])      &&  $data['place']      =   $cloudInfo['place'];
        isset($cloudInfo['status'])     &&  $data['status']     =   $cloudInfo['status'];
        isset($cloudInfo['int1'])       &&  $data['int1']       =   $cloudInfo['int1'];
        isset($cloudInfo['int2'])       &&  $data['int2']       =   $cloudInfo['int2'];
        isset($cloudInfo['int3'])       &&  $data['int3']       =   $cloudInfo['int3'];
        isset($cloudInfo['float1'])     &&  $data['float1']     =   $cloudInfo['float1'];
        isset($cloudInfo['float2'])     &&  $data['float2']     =   $cloudInfo['float2'];
        isset($cloudInfo['float3'])     &&  $data['float3']     =   $cloudInfo['float3'];
        isset($cloudInfo['string1'])    &&  $data['string1']    =   $cloudInfo['string1'];
        isset($cloudInfo['string2'])    &&  $data['string2']    =   $cloudInfo['string2'];
        isset($cloudInfo['string3'])    &&  $data['string3']    =   $cloudInfo['string3'];
        isset($cloudInfo['string4'])    &&  $data['string4']    =   $cloudInfo['string4'];
        isset($cloudInfo['string5'])    &&  $data['string5']    =   $cloudInfo['string5'];
        isset($cloudInfo['string6'])    &&  $data['string6']    =   $cloudInfo['string6'];
        isset($cloudInfo['label'])      &&  $data['label']      =   $cloudInfo['label'];
        isset($cloudInfo['labels'])     &&  $data['labels']     =   $cloudInfo['labels'];
        if (!empty($cloudInfo['paper']) && $data['type'] == 1){
            $paper = json_encode(array('paper' => $cloudInfo['paper']));
            $data['string1'] = $paper;
        }

        $key    =   $this->cloudSyncContentKeyPrefix. $cloudInfo['uid'];
        $result =   Storage::addData($key, $data);

        if (empty($result))
        {
            AC::$errorNo    =   'CSCM004';
        }
        return  $result;
    }/*}}}*/

    public function getBodyId($uid, $cscId)/*{{{*/
    {
        $key    =   $this->cloudSyncContentKeyPrefix. $uid;
        $bodyId =   UK::getBodyidByItemid($key, $cscId);

        if (empty($bodyId))
        {
            AC::$errorNo    =   'CSCM005';
            return  FALSE;
        }
        return  $bodyId;
    }/*}}}*/
    
    public function getSyncDetail($uid, $bodyId, $type=1){
    	$key    =   $this->cloudSyncContentKeyPrefix. $uid;
    	if (empty($bodyId)){
    		AC::$errorNo = 'CSCM001';
    		return FALSE;
    	}
    	//$bodyId = $this->getCscId($bodyId);
    	$detail = Storage::getConditionData($key, [
    			'id' => $bodyId,
    			'uid' => $uid,
    			'type' => $type,
                'status' => 0
    	]);
    	return $detail;
    }

    public function getCscId($bodyId)/*{{{*/
    {
        $info   =   UK::getItemidByBodyid($bodyId);
        if (empty($info['itemId']))
        {
            AC::$errorNo    =   'CSCM006';
            return  FALSE;
        }
        return  $info['itemId'];
    }/*}}}*/

    public function cloudSyncCount($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']))
        {
            AC::$errorNo    =   'CSCM002';
            return  FALSE;
        }

        $key    =   $this->cloudSyncContentKeyPrefix. $cloudInfo['uid'];
        $fields =   ' count(*) as num ';
        $where  =   array('uid' => $cloudInfo['uid'], 'status' => 0);

        $result =   Storage::getFieldsConditionManyData($key, $fields, $where);

        if (empty($result[0]['num']))   return  array('num' =>  0);
        return  $result[0];
    }/*}}}*/

    public function cloudSyncList($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']))
        {
            AC::$errorNo    =   'CSCM002';
            return  FALSE;
        }
        if (!is_numeric($cloudInfo['saveTime'])) {
            $cloudInfo['saveTime'] = 0;
        }

        $key    =   $this->cloudSyncContentKeyPrefix. $cloudInfo['uid'];
        $fields =   array('id', 'version', 'saveTime', 'status');
        if (empty($cloudInfo['saveTime']))
        {
            $where  =   ' uid = "'. $cloudInfo['uid']. '" AND status = 0 ';
        }
        else
        {
            $where  =   ' uid = "'. $cloudInfo['uid']. '" AND saveTime > '. $cloudInfo['saveTime'];
        }

        if (!empty($cloudInfo['in']))
        {
            $where  =   $where. ' AND id IN ('. $cloudInfo['in']. ') ';
        }

        $orderBy=   ' saveTime ASC ';
        $result =   Storage::getFieldsConditionManyData($key, $fields, $where, $orderBy);
        return  $result;
    }/*}}}*/

    public function cloudDownload($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']))
        {
            AC::$errorNo    =   'CSCM002';
            return  FALSE;
        }
        $start  =   isset($cloudInfo['start'])  ?   $cloudInfo['start'] :   0;
        $length =   isset($cloudInfo['length']) ?   $cloudInfo['length']:   AC::CLOUD_SYNC_DISPLAY_LEN;
        if ($start  <   0)  $start  =   0;
        $key    =   $this->cloudSyncContentKeyPrefix. $cloudInfo['uid'];
        $where  =   ' uid = "'. $cloudInfo['uid']. '" ';
        if (empty($cloudInfo['saveTime']))
        {
            $where  =   $where. ' AND status = 0 ';
        }
        else
        {
            $where  =   $where. ' AND saveTime > '. ($cloudInfo['saveTime'] -1);
        }
        $orderBy=   ' saveTime ASC ';
        #   $orderBy=   ' markDate DESC ';

        if (!empty($cloudInfo['in']))
        {
            # $where  =   ' id IN ('. $cloudInfo['in']. ') AND uid = "'. $cloudInfo['uid']. '"';
            $where  =   $where. ' AND id IN ('. $cloudInfo['in']. ') ';
        }

        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return  $result;
    }/*}}}*/

    public function cloudUpdate($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']) || empty($cloudInfo['type'])   ||  empty($cloudInfo['id']))
        {
            AC::$errorNo    =   'CSCM001';
            return  FALSE;
        }
        $checkType  =   $this->checkType($cloudInfo['type']);
        if (empty($checkType))  return  FALSE;

        $data['type']       =   $cloudInfo['type'];
        $data['saveTime']   =   AC::SERVER_TIME;

        isset($cloudInfo['title'])      &&  $data['title']      =   $cloudInfo['title'];
        isset($cloudInfo['content'])    &&  $data['content']    =   $cloudInfo['content'];
        isset($cloudInfo['markDate'])   &&  $data['markDate']   =   $cloudInfo['markDate'];
        isset($cloudInfo['category'])   &&  $data['category']   =   abs($cloudInfo['category']);
        isset($cloudInfo['secret'])     &&  $data['secret']     =   abs($cloudInfo['secret']);
        isset($cloudInfo['weather'])    &&  $data['weather']    =   abs($cloudInfo['weather']);
        isset($cloudInfo['emotion'])    &&  $data['emotion']    =   abs($cloudInfo['emotion']);
        isset($cloudInfo['textSize'])   &&  $data['textSize']   =   abs($cloudInfo['textSize']);
        isset($cloudInfo['textColor'])  &&  $data['textColor']  =   $cloudInfo['textColor'];
        isset($cloudInfo['theme'])      &&  $data['theme']      =   abs($cloudInfo['theme']);
        isset($cloudInfo['posX'])       &&  $data['posX']       =   $cloudInfo['posX'];
        isset($cloudInfo['posY'])       &&  $data['posY']       =   $cloudInfo['posY'];
        isset($cloudInfo['place'])      &&  $data['place']      =   $cloudInfo['place'];
        isset($cloudInfo['status'])     &&  $data['status']     =   $cloudInfo['status'];
        isset($cloudInfo['int1'])       &&  $data['int1']       =   $cloudInfo['int1'];
        isset($cloudInfo['int2'])       &&  $data['int2']       =   $cloudInfo['int2'];
        isset($cloudInfo['int3'])       &&  $data['int3']       =   $cloudInfo['int3'];
        isset($cloudInfo['float1'])     &&  $data['float1']     =   $cloudInfo['float1'];
        isset($cloudInfo['float2'])     &&  $data['float2']     =   $cloudInfo['float2'];
        isset($cloudInfo['float3'])     &&  $data['float3']     =   $cloudInfo['float3'];
        isset($cloudInfo['string1'])    &&  $data['string1']    =   $cloudInfo['string1'];
        isset($cloudInfo['string2'])    &&  $data['string2']    =   $cloudInfo['string2'];
        isset($cloudInfo['string3'])    &&  $data['string3']    =   $cloudInfo['string3'];
        isset($cloudInfo['string4'])    &&  $data['string4']    =   $cloudInfo['string4'];
        isset($cloudInfo['string5'])    &&  $data['string5']    =   $cloudInfo['string5'];
        isset($cloudInfo['string6'])    &&  $data['string6']    =   $cloudInfo['string6'];
        isset($cloudInfo['label'])      &&  $data['label']      =   abs($cloudInfo['label']);
        isset($cloudInfo['labels'])     &&  $data['labels']     =   $cloudInfo['labels'];

        $key    =   $this->cloudSyncContentKeyPrefix. $cloudInfo['uid'];
        $where  =   array('id' => $cloudInfo['id'], 'uid' => $cloudInfo['uid']);
        $result =   Storage::setConditionData($key, $data, $where);

        if (FALSE   === $result)    AC::$errorNo    =   'CSCM007';
        $this->updateTimes($cloudInfo);

        return  $result;
    }/*}}}*/

    public function cloudRemove($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']) ||  (empty($cloudInfo['id']) && empty($cloudInfo['in'])))
        {
            AC::$errorNo    =   'CSCM001';
            return  FALSE;
        }
        # $checkType  =   $this->checkType($cloudInfo['type']);
        # if (empty($checkType))  return  FALSE;

        $key        =   $this->cloudSyncContentKeyPrefix. $cloudInfo['uid'];
        if (empty($cloudInfo['in']))
        {
            $where  =   array('id' => $cloudInfo['id'], 'uid'   =>  $cloudInfo['uid']);
        }
        else
        {
            $where  =   ' id IN ('. $cloudInfo['in']. ') AND uid="'. $cloudInfo['uid']. '" ';
        }
        #   $result =   Storage::delConditionData($key, $where);
        $data   =   array('status' => 1, 'saveTime' => AC::SERVER_TIME);
        $result =   Storage::setConditionData($key, $data, $where);
        if (empty($result)) AC::$errorNo    =   'CSCM008';

        return  TRUE;
    }/*}}}*/
    
    #恢复用户同步信息        /*{{{*/
    public function regainCloudSync($cloudInfo)
    {
        if (empty($cloudInfo['uid']))
        {
            AC::$errorNo    =   'CSCM001';
            return  FALSE;
        }
        
        $key        =   $this->cloudSyncContentKeyPrefix. $cloudInfo['uid'];
        
        #获取已删除的信息id
        $fields     =   array('id');
        $getWhere   =   array('uid' => $cloudInfo['uid'],'status' => 1);
        $cloudId    =   Storage::getFieldsConditionManyData($key, $fields, $getWhere);
        
        if (empty($cloudId)   ||  !is_array($cloudId))
        {
            AC::$errorNo    =   'CSCM003';
            return  FALSE;
        }
        
        $ids        =    array();
        foreach ($cloudId as $value)
        {
            foreach ($value as $v)
            {
                $ids[]=$v;
            }
        }
        $id         =   implode(',', $ids);
        $where      =   ' id IN ('. $id. ') AND uid="'. $cloudInfo['uid']. '" ';
        $data       =   array('status' => 0, 'saveTime' => AC::SERVER_TIME);
        $result     =   Storage::setConditionData($key, $data, $where);
        
        if (empty($result)) AC::$errorNo    =   'CSCM004';
        
        return  TRUE;
    }/*}}}*/

    public function cloudRemoveByUid($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']))
        {
            AC::$errorNo    =   'CSCM010';
            return  FALSE;
        }
        # $checkType  =   $this->checkType($cloudInfo['type']);
        # if (empty($checkType))  return  FALSE;

        $key    =   $this->cloudSyncContentKeyPrefix. $cloudInfo['uid'];
        $where  =   array('uid' => $cloudInfo['uid']);
        $result =   Storage::delConditionData($key, $where);
        if (empty($result)) AC::$errorNo    =   'CSCM008';

        return  TRUE;
    }/*}}}*/

    private function updateTimes($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']) || empty($cloudInfo['type'])   ||  empty($cloudInfo['id']))
        {
            AC::$errorNo    =   'CSCM001';
            return  FALSE;
        }

        $data   =   array('version' => 1);
        $key    =   $this->cloudSyncContentKeyPrefix. $cloudInfo['uid'];
        $where  =   array('id' => $cloudInfo['id'], 'uid' => $cloudInfo['uid']);

        $result =   Storage::changeNumValueAdd($key, $data, $where);
        return  $result;
    }/*}}}*/

    private function checkType($type)/*{{{*/
    {
        return  TRUE;
        $cloudTypeArray =   array(
                                AC::FEED_DIARY              =>  1,
                                AC::FEED_KIT_ANNIVERSARY    =>  12,
                                AC::FEED_KIT_BMI            =>  14,
                                AC::FEED_KIT_MENSES         =>  15,
                                AC::FEED_KIT_NOTE           =>  9,
                                AC::FEED_KIT_SCHEDULE       =>  10,
                                AC::FEED_KIT_TALLYBOOK      =>  8,
                            );
        if (empty($cloudTypeArray[$type]))
        {
            AC::$errorNo    =   'CSCM003';
            return  FALSE;
        }
        return  $cloudTypeArray[$type];
    }/*}}}*/

}

